﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using Sio.Mdm.Types;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Administracija;

namespace Sio.Mdm.Web
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Sio.Mdm.Server.Server.Initialize();
            Sio.Mdm.Utils.Logger.UseSeparateThread = false;
            Sio.Mdm.Server.Server.Interceptors.Add(new Base.SecurityTokenWriter());
            SessionFactory.RegisterProvider(new Base.SessionProvider());
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            
        }
        
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.RawUrl.ToLower();

            // HACK: This is workaround for forms authentication
            // requests to ext handler should be allowed all the time
            if (url.Contains("ext.axd") || url.Contains("database.aspx"))
            {
                HttpContext.Current.SkipAuthorization = true;
            }
        }

        protected void Application_Error(object sender, EventArgs e)
        {   
        }        

        protected void Session_End(object sender, EventArgs e)
        {
        }        

        protected void Application_End(object sender, EventArgs e)
        {
            Sio.Mdm.Server.Server.Destroy();
        }
    }
}